package com.example.jasonsun.sharelib.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jasonsun.sharelib.PopupDialogShare;
import com.example.jasonsun.sharelib.R;
import com.example.jasonsun.sharelib.config.Cache;
import com.example.jasonsun.sharelib.platform.QQPlatform;
import com.example.jasonsun.sharelib.platform.QZonePlatform;
import com.example.jasonsun.sharelib.platform.SharePlatform;
import com.sina.weibo.sdk.api.share.BaseResponse;
import com.sina.weibo.sdk.api.share.IWeiboHandler;
import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
import com.sina.weibo.sdk.api.share.WeiboShareSDK;
import com.sina.weibo.sdk.constant.WBConstants;
import com.tencent.tauth.Tencent;

/**
 * 分享平台选择界面
 */
public class ShareActivity extends Activity implements IWeiboHandler.Response {
    private PopupDialogShare.onShareModelSelectedListener mListener;
    private String mTitle = "test title";
    private String mContent = "hello content , this is for test";
    private String mImgUrl = "http://m.img10.starclouds.com.cn/20160601/a8095135-f386-45fd-8d28-a5c7bc149340_1.jpg@100h_100w";
//    private String mImgUrl = "http://m.img10.starclouds.com.cn/20160601/a8095135-f386-45fd-8d28-a5c7bc149340_1.jpg";
    private String mWebUrl = "http://192.168.199.234:10010/nshare/show.do?projectId=215&shareDes=%E7%83%A6%E7%83%A6%E7%83%A6%E5%8F%91";
    /**
     * 微博微博分享接口实例
     */
    private IWeiboShareAPI mWeiboShareAPI = null;
    private SharePlatform mPlatform;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FrameLayout fl = new FrameLayout(this);
        final TextView v = new TextView(this);
        v.setAlpha(0);
        fl.addView(v, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
        setContentView(fl);
        setListener();
        final PopupDialogShare popupDialogShare = new PopupDialogShare(ShareActivity.this, mListener);
        popupDialogShare.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                finish();
            }
        });

        v.post(new Runnable() {
            @Override
            public void run() {
                popupDialogShare.showPopup(v);
            }
        });

        // 创建微博分享接口实例
        mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(this, Cache.sina_app_key);
//        mWeiboShareAPI.registerApp();

        // 当 Activity 被重新初始化时（该 Activity 处于后台时，可能会由于内存不足被杀掉了），
        // 需要调用 {@link IWeiboShareAPI#handleWeiboResponse} 来接收微博客户端返回的数据。
        // 执行成功，返回 true，并调用 {@link IWeiboHandler.Response#onResponse}；
        // 失败返回 false，不调用上述回调
        if (savedInstanceState != null) {
            mWeiboShareAPI.handleWeiboResponse(getIntent(), this);
        }
    }

    private void setListener() {
//        mListener = new PopupDialogShare.onShareModelSelectedListener() {
//            @Override
//            public void onSelected(int shareTo) {
//                mPlatform = null;
//                int platformId = shareTo;
//                switch (shareTo) {
//                    case SharePlatform.XINLANG_WEIBO:
//                        mPlatform = new XlWeiboPlatform(ShareActivity.this, mWeiboShareAPI);
//                        break;
//                    case SharePlatform.QQ_FRIENDS:
//                        mPlatform = new QQPlatform(ShareActivity.this);
//                        break;
//                    case SharePlatform.QZONE:
//                        mPlatform = new QZonePlatform(ShareActivity.this);
//                        break;
//                }
//
//                mPlatform.share(platformId, mTitle, mContent, mImgUrl, mWebUrl);
//            }
//        };
    }

    /**
     * @see {@link Activity#onNewIntent}
     */
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        // 从当前应用唤起微博并进行分享后，返回到当前应用时，需要在此处调用该函数
        // 来接收微博客户端返回的数据；执行成功，返回 true，并调用
        // {@link IWeiboHandler.Response#onResponse}；失败返回 false，不调用上述回调
        mWeiboShareAPI.handleWeiboResponse(intent, this);
    }

    /**
     * 接收微客户端博请求的数据。
     * 当微博客户端唤起当前应用并进行分享时，该方法被调用。
     *
     * @param baseResp 微博请求数据对象
     * @see {@link IWeiboShareAPI#handleWeiboRequest}
     */
    @Override
    public void onResponse(BaseResponse baseResp) {
        if (baseResp != null) {
            switch (baseResp.errCode) {
                case WBConstants.ErrorCode.ERR_OK:
                    Toast.makeText(this, R.string.weibosdk_demo_toast_share_success, Toast.LENGTH_LONG).show();
                    break;
                case WBConstants.ErrorCode.ERR_CANCEL:
                    Toast.makeText(this, R.string.weibosdk_demo_toast_share_canceled, Toast.LENGTH_LONG).show();
                    break;
                case WBConstants.ErrorCode.ERR_FAIL:
                    Toast.makeText(this,
                            getString(R.string.weibosdk_demo_toast_share_failed) + "Error Message: " + baseResp.errMsg,
                            Toast.LENGTH_LONG).show();
                    break;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == com.tencent.connect.common.Constants.REQUEST_QQ_SHARE) {
            Tencent.onActivityResultData(requestCode, resultCode, data, ((QQPlatform) mPlatform).qqShareListener);
        } else if (requestCode == com.tencent.connect.common.Constants.REQUEST_QZONE_SHARE) {
            Tencent.onActivityResultData(requestCode, resultCode, data, ((QZonePlatform) mPlatform).qZoneShareListener);
        }
    }
}
